package com.pounds.view;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.pounds.draggedview.R;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/4/20.
 */
public class ICBCButtonAdapter extends ArrayAdapter {
    private List<ButtonResponseBean> originlist=new ArrayList<>();
    private List<ButtonResponseBean> realList=new ArrayList<>();
    List<ButtonResponseBean> contentList=new ArrayList<>();
    private Context mContext;
    private LayoutInflater inflater;
    private ImageView iv;
    private TextView tv;
    private int resourceid;
    private boolean isFirstTime=true;
    public static boolean canDrag=false;

    public ICBCButtonAdapter(Context mContext, List<ButtonResponseBean> list, int resourceid){
        super(mContext,resourceid);
        this.originlist.clear();
        originlist.addAll(list);
        this.mContext=mContext;
        this.resourceid=resourceid;
        inflater = LayoutInflater.from(mContext);
        hideItems();
    }

    @Override
    public int getCount() {
        return realList.size();
    }

    @Override
    public ButtonResponseBean getItem(int position) {
        return realList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final String text = realList.get(position).getButtonTitle();
        final String pic = realList.get(position).getButtonPic();
        if (convertView == null) {
            convertView = inflater.inflate(resourceid, null);
        }
        iv=(ImageView)convertView.findViewById(
                R.id.iv_icbc_button);
        tv=(TextView)convertView.findViewById(R.id.tv_icbc_button);
        if (position != 4) {
            Picasso.with(mContext).load(pic).into(iv);
            tv.setText(text);
        } else {
            tv.setText("更多");
            if (canDrag) {
                iv.setImageResource(R.drawable.retract);
            } else {
                iv.setImageResource(R.drawable.more);
            }

        }
        return convertView;
    }

    public void swapItem(int oldIndex,int newIndex){

        ButtonResponseBean itemMore = realList.get(4);
        ButtonResponseBean item = realList.get(oldIndex);
        realList.remove(item);
        realList.add(newIndex,item);
        realList.remove(itemMore);
        realList.add(4,itemMore);
        contentList.clear();
        contentList.addAll(realList);
        contentList.remove(contentList.get(4));

    }

    public void hideItems(){
        canDrag=false;
        if(isFirstTime) {
            realList.clear();
            for (int i = 0; i < 4; i++) {
                realList.add(originlist.get(i));
            }
            ButtonResponseBean ib = new ButtonResponseBean();
            realList.add(ib);
            contentList.clear();
            contentList.addAll(originlist);
            notifyDataSetChanged();
            isFirstTime=false;
        }else{
            realList.clear();
            for (int i = 0; i < 4; i++) {
                realList.add(contentList.get(i));
            }
            ButtonResponseBean ib = new ButtonResponseBean();
            realList.add(ib);
            notifyDataSetChanged();
        }
    }

    public void showItems(){
        canDrag=true;
        realList.clear();
        realList.addAll(contentList);
        ButtonResponseBean ib=new ButtonResponseBean();
        realList.add(4,ib);
        notifyDataSetChanged();
    }

    public List<ButtonResponseBean> getContentList(){

        return contentList;
    }

    public boolean canDraggable(){
        return canDrag;
    }

    public void reLoadList(List<ButtonResponseBean> list){
        this.originlist.clear();
        originlist.addAll(list);
        isFirstTime=true;
        hideItems();
    }

}
